package com.cdht.util;


public class CommonUtils {
    // 手机号码前三后四脱敏
    public static String mobileEncrypt(String mobile) {
        if (StringUtils.isEmpty(mobile) || mobile.length() <=4) {
            return mobile;
        }else if(mobile.length() == 11 ) {
            return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        }else if(mobile.length() > 4 ) {
            StringBuffer buffer = new StringBuffer();
            buffer.append(mobile.substring(0,3));
            for (int i = 0; i < mobile.length() - 3 - 2; i++) {
                buffer.append("*");  
            }
            buffer.append(mobile.substring(mobile.length() - 2));
            return buffer.toString();
        }
        
        return "";
    }

    //身份证前三后四脱敏
    public static String idEncrypt(String id) {
        if (StringUtils.isEmpty(id) || id.length() < 8 ) { //等于空或者小于8位直接显示
            return id;
        }else if(id.length() == 18) {//18位则显示前3后4
            return id.replaceAll("(?<=\\w{3})\\w(?=\\w{4})", "*");
        } 
        
        StringBuffer buffer = new StringBuffer();
        buffer.append(id.substring(0,3));
        for (int i = 0; i < id.length() - 3 - 2; i++) {
            buffer.append("*");  
        }
        buffer.append(id.substring(id.length() - 3));
        return buffer.toString();
    }

    
    public static void main(String[] args) {
        System.out.println(mobileEncrypt("13198830129"));
        System.out.println(mobileEncrypt("1319883001"));
        System.out.println(mobileEncrypt("131"));
        System.out.println(mobileEncrypt("1312"));
        System.out.println(mobileEncrypt("13121"));
        System.out.println(mobileEncrypt("131212"));
        
        System.out.println(idEncrypt("12345678"));
        System.out.println(idEncrypt("123456789"));
        System.out.println(idEncrypt("511513192307290617"));
        System.out.println(idEncrypt("5111231991072906371"));
    }

}